<template>
  <el-card class="box-card">
    <el-form
        ref="ruleForm"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
    >
      <el-form-item label="教师姓名" prop="name">
        <el-input
            v-model="ruleForm.name"
            autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm()">确认</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { ElMessage } from 'element-plus'
import {findUser} from "@/utils/api";

export default {
  name: "Ter_in_Form",
  data() {
    return {
      ruleForm: {},
    }
  },

  methods: {
    // 确认按钮
    submitForm() {
      var fname = this.ruleForm.name //记录表单接收的名字和密码
      var fpass = this.ruleForm.password
      findUser("ter", fname, fpass).then(res => {
        switch (res.code){
          // 查找到用户就将用户信息存入数据库
          case "0":
            sessionStorage.setItem("1", JSON.stringify(res.data))
            sessionStorage.setItem('2', '教师')
            ElMessage.success('欢迎！'+fname+'老师!')
            setTimeout(()=>{
              location.reload()
              //延迟1秒刷新
            },1000)
            break
          // 未找到用户
          case "1":
            ElMessage.error('账号或密码错误！')
        }
      })
    },
    // 清空表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style>

.box-card {
  width: auto;
  height: auto;
}
</style>